package com.cdyx.outsideprescription.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cdyx.outsideprescription.dto.YPxxDto;
import com.cdyx.outsideprescription.entity.TbDicDrug;
import com.cdyx.outsideprescription.service.ITbDicDrugService;
import com.cdyx.outsideprescription.util.AppResponse;
import com.cdyx.outsideprescription.util.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
@CrossOrigin
@RestController
@RequestMapping("/drug")
@Api("药品字典维护")
public class DIcDrugController {
    @Qualifier("dicDrugServiceImpl")
    @Autowired
    private ITbDicDrugService dicDrugService;
    @ApiOperation("新增药品")
    @PutMapping("/addDrug")
    public AppResponse addDrug(@RequestBody TbDicDrug dicDrug){
        try {
            dicDrugService.insert(dicDrug);
        }catch (Exception e){
            e.printStackTrace();
            return AppResponse.fail();
        }
       return AppResponse.ok();
    }
    @ApiOperation("修改药品信息")
    @PutMapping("/modifyDrug")
    public AppResponse modifyDrug(@RequestBody TbDicDrug dicDrug){
        boolean b=false;
        try {
            b = dicDrugService.updateById(dicDrug);
        }catch (Exception e){
            e.printStackTrace();
            return AppResponse.fail();
        }
        if (b)
            return AppResponse.ok();
        else
            return AppResponse.fail();
    }
    @ApiOperation("查询药品信息")
    @ApiImplicitParams(
            @ApiImplicitParam(name = "data",//参数名字
                    value = "这是一个添加",//参数的描述
                    required = true,//是否是必须要传的参数，true是
                    paramType = "body",//参数类型 body 代表json
                    example = "{\"limit\":10,\"current\":1,\"condition\":\"aa\"}"
            )
    )
    @PostMapping("/queryDrug")
    public AppResponse queryDrug(@RequestBody Map<String,String> data){
        String condition = data.getOrDefault("condition", "").toString();
        int limit = Integer.parseInt(data.getOrDefault("limit", "10").toString());
        int current = Integer.parseInt(data.getOrDefault("current", "1").toString());
        String ypzlfl = data.getOrDefault("ypzlfl", "");
        String prodname = data.getOrDefault("prodname", "");
        String pym = data.getOrDefault("PYM","");
        String hospitalMedicineCode = data.getOrDefault("hospitalMedicineCode", "");
        QueryWrapper<TbDicDrug> queryWrapper=new QueryWrapper<>();
        queryWrapper.like(!ypzlfl.isEmpty(),"YPZLFL",ypzlfl)
                .or().like(!prodname.isEmpty(),"prodname",prodname)
                .or().like(!pym.isEmpty(),"PYM",pym)
                .or().like(!hospitalMedicineCode.isEmpty(),"hospitalMedicineCode",hospitalMedicineCode);
        Page<TbDicDrug> page=new Page<>(current,limit,true);
        IPage<TbDicDrug> iPage= dicDrugService.selectDrug(page,queryWrapper);
        Map<String, Long> pageInfo = PageInfo.getPageInfo(iPage);
        List<TbDicDrug> records = iPage.getRecords();
        Map<String,Object> ypxx=new HashMap<>();
        ypxx.put("ypxx",records);
        ypxx.put("pageInfo",pageInfo);
        return AppResponse.ok(ypxx);
    }
    @ApiOperation("删除药品信息")
    @DeleteMapping("/deleteById/{id}")
    public AppResponse deleteById(@PathVariable int id){
        boolean b = dicDrugService.removeById(id);
        if (b)
            return AppResponse.ok();
        else return AppResponse.fail();
    }
}
